package com.telecom.cms.dbconnect;

import com.telecom.cms.util.Constant;

public class Chinese {
	public static String toChinese(String strvalue) {
		try {
			if (strvalue == null || strvalue.equals("null")) {
				return "";
			} else {
				if (Constant.myContainer.equalsIgnoreCase("TOMCAT"))
				     strvalue = new String(strvalue.getBytes("ISO-8859-1"), "GB2312");
				String str = strvalue.replace((char) 39, (char) 34);
				return str;
			}
		} catch (Exception e) {
			return "";
		}
	}
    
	public static String decode(String s,String encoding) throws Exception { 
		StringBuffer sb = new StringBuffer(); 
		for(int i=0; i<s.length();i++)
		{ 
		char c = s.charAt(i); 
		switch (c) { 
		case '+': 
		sb.append(' '); 
		break; 
		case '%': 
		try { 
		sb.append((char)Integer.parseInt( 
		s.substring(i+1,i+3),16)); 
		} 
		catch (NumberFormatException e) { 
		throw new IllegalArgumentException(); 
		} 
		i += 2; 
		break; 
		default: 
		sb.append(c); 
		break; 
		} 
		} 
		String result = sb.toString(); 
		byte[] inputBytes = result.getBytes("ISO-8859-1"); 
		return new String(inputBytes,encoding); 
		} 
	public static String toChinaRs(String strvalue) {
		try {
			if (strvalue == null || strvalue.equals("null")) {
				return "";
			} else {
				//if (Constant.myContainer.equalsIgnoreCase("TOMCAT"))
				//    strvalue = new String(strvalue.getBytes("ISO-8859-1"), "UTF-8");
				String str = strvalue.replace((char) 39, (char) 34);
				return str;
			}
		} catch (Exception e) {
			return "";
		}
	}
	
	public static String toChina(String strvalue) {
		try {
			if (strvalue == null || strvalue.equals("null")) {
				return "";
			} else {
				if (Constant.myContainer.equalsIgnoreCase("TOMCAT"))
				    strvalue = new String(strvalue.getBytes("ISO-8859-1"), "UTF-8");
				String str = strvalue.replace((char) 39, (char) 34);
				return str;
			}
		} catch (Exception e) {
			return "";
		}
	}

	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;

				try {
					b = ("" + c).getBytes("utf-8");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}

				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
	
	public static String gb2utf(String inPara){ 

		  char temChr; 

		  int ascChr; 

		  int i; 

		  String rtStr=new String(""); 

		  if(inPara==null){ 

		   inPara=""; 

		  } 

		  for(i=0;i<inPara.length();i++){ 

		   temChr=inPara.charAt(i); 

		   ascChr=temChr+0; 

//		   System.out.println(ascChr); 

//		   System.out.println(Integer.toBinaryString(ascChr)); 

		   rtStr=rtStr+"&#x"+Integer.toHexString(ascChr)+";"; 

		  } 

		  return rtStr; 

		}
}
